{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Start Here\n",
    "\n",
    "In the cell below are a few functions that help with plotting data using `matplotlib`. Run the cell to define the functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"PROJ_LIB\"] = \"/opt/conda/share/proj\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def show_plot(x_data, y_data, x_label, y_label):\n",
    "    \"\"\"\n",
    "    Display a simple line plot.\n",
    "    \n",
    "    :param x_data: Numpy array containing data for the X axis\n",
    "    :param y_data: Numpy array containing data for the Y axis\n",
    "    :param x_label: Label applied to X axis\n",
    "    :param y_label: Label applied to Y axis\n",
    "    \"\"\"\n",
    "    plt.figure(figsize=(10,5), dpi=100)\n",
    "    plt.plot(x_data, y_data, 'b-', marker='|', markersize=2.0, mfc='b')\n",
    "    plt.grid(b=True, which='major', color='k', linestyle='-')\n",
    "    plt.xlabel(x_label)\n",
    "    plt.ylabel (y_label)\n",
    "    plt.show()\n",
    "    \n",
    "def plot_box(bbox):\n",
    "    \"\"\"\n",
    "    Display a Green bounding box on an image of the blue marble.\n",
    "    \n",
    "    :param bbox: Shapely Polygon that defines the bounding box to display\n",
    "    \"\"\"\n",
    "    min_lon, min_lat, max_lon, max_lat = bbox.bounds\n",
    "    import matplotlib.pyplot as plt1\n",
    "    from matplotlib.patches import Polygon\n",
    "    from mpl_toolkits.basemap import Basemap\n",
    "\n",
    "    map = Basemap()\n",
    "    map.bluemarble(scale=0.5)\n",
    "    poly = Polygon([(min_lon,min_lat),(min_lon,max_lat),(max_lon,max_lat),(max_lon,min_lat)],\n",
    "                   facecolor=(0,0,0,0.0),edgecolor='green',linewidth=2)\n",
    "    plt1.gca().add_patch(poly)\n",
    "    plt1.gcf().set_size_inches(10,15)\n",
    "    \n",
    "    plt1.show()\n",
    "    \n",
    "def show_plot_two_series(x_data_a, x_data_b, y_data_a, y_data_b, x_label, \n",
    "                         y_label_a, y_label_b, series_a_label, series_b_label):\n",
    "    \"\"\"\n",
    "    Display a line plot of two series\n",
    "    \n",
    "    :param x_data_a: Numpy array containing data for the Series A X axis\n",
    "    :param x_data_b: Numpy array containing data for the Series B X axis\n",
    "    :param y_data_a: Numpy array containing data for the Series A Y axis\n",
    "    :param y_data_b: Numpy array containing data for the Series B Y axis\n",
    "    :param x_label: Label applied to X axis\n",
    "    :param y_label_a: Label applied to Y axis for Series A\n",
    "    :param y_label_b: Label applied to Y axis for Series B\n",
    "    :param series_a_label: Name of Series A\n",
    "    :param series_b_label: Name of Series B\n",
    "    \"\"\"\n",
    "    fig, ax1 = plt.subplots(figsize=(10,5), dpi=100)\n",
    "    series_a, = ax1.plot(x_data_a, y_data_a, 'b-', marker='|', markersize=2.0, mfc='b', label=series_a_label)\n",
    "    ax1.set_ylabel(y_label_a, color='b')\n",
    "    ax1.tick_params('y', colors='b')\n",
    "    ax1.set_ylim(min(0, *y_data_a), max(y_data_a)+.1*max(y_data_a))\n",
    "    ax1.set_xlabel(x_label)\n",
    "    \n",
    "    ax2 = ax1.twinx()\n",
    "    series_b, = ax2.plot(x_data_b, y_data_b, 'r-', marker='|', markersize=2.0, mfc='r', label=series_b_label)\n",
    "    ax2.set_ylabel(y_label_b, color='r')\n",
    "    ax2.set_ylim(min(0, *y_data_b), max(y_data_b)+.1*max(y_data_b))\n",
    "    ax2.tick_params('y', colors='r')\n",
    "    \n",
    "    plt.grid(b=True, which='major', color='k', linestyle='-')\n",
    "    plt.legend(handles=(series_a, series_b), bbox_to_anchor=(1.1, 1), loc=2, borderaxespad=0.)\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Run Time Series and Plot\n",
    "\n",
    "In the cell below we import the `nexuscli` library and target the NEXUS webservice running in the `nexus-webapp` container.  \n",
    "\n",
    "Then we define a bounding box and plot it on a map using the `plot_box` function defined above.  \n",
    "\n",
    "Next, we define the time bounds for our time series and submit the request to NEXUS. The request is timed and the results are then plotted using the `show_plot` method defined above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import nexuscli\n",
    "from datetime import datetime\n",
    "\n",
    "from shapely.geometry import box\n",
    "\n",
    "nexuscli.set_target(\"http://nexus-webapp:8083\")\n",
    "\n",
    "# Create a bounding box using the box method imported above\n",
    "bbox = box(-150, 40, -120, 55)\n",
    "\n",
    "# Plot the bounding box using the helper method plot_box\n",
    "plot_box(bbox)\n",
    "\n",
    "start = time.perf_counter()\n",
    "\n",
    "# Call the time_series method for the AVHRR_OI_L4_GHRSST_NCEI dataset using \n",
    "# the bounding box and time period 2013-01-01 through 2014-03-01\n",
    "datasets = [\"AVHRR_OI_L4_GHRSST_NCEI\"]\n",
    "start_time = datetime(2015, 11, 1)\n",
    "end_time = datetime(2015, 11, 30)\n",
    "ts = nexuscli.time_series(datasets, bbox, start_time, end_time, spark=True)\n",
    "\n",
    "print(\"Time Series took {} seconds to generate\".format(time.perf_counter() - start))\n",
    "\n",
    "# Plot the resulting time series using the helper method show_plot\n",
    "avhrr_ts = ts[0]\n",
    "show_plot(avhrr_ts.time, avhrr_ts.mean, 'Time', 'Temperature (C)')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}